define([
  ELMP.resource('utils/axios.min.js', 'lib'),
  ELMP.resource('utils/download.js', 'lib'),
], function (axios, download) {
  'use strict'
  const service = axios.create({
    baseURL: '',
    timeout: 5 * 60 * 60 * 1000, // request timeout
    headers: { 'Content-Type': 'application/json;charset=UTF-8' }
  })
  // 'Content-Type': 'multipart/form-data'
  // request interceptor
  service.interceptors.request.use(
    config => {
      if (config.formData) {
        config.headers['Content-Type'] = 'multipart/form-data'
      }
      Object.assign(config.headers, $.el.util.getDefaultAjaxHeaders())
      return config
    },
    error => {
      // do something with request error
      return Promise.reject(error)
    }
  )
  // response interceptor
  service.interceptors.response.use(
    response => {
      if (response.config.responseAll) {
        return response
      }
      if (response.config.responseType === 'blob') {
        download.downloadResopnse(response)
        return
      }
      if (response.data?.success === false) {
        $.msg.error(response.data.message)
        return Promise.reject(response.data.message)
      }
      const res = response.data?.res
      if (res && Object.keys(res).length > 1) {
        return res
      }
      return res?.data
    },
    error => {
      let message
      if (error.response?.data?.message) {
        message = error.response.data.message
      } else {
        message = error.message
      }
      $.msg.error(message)
      return Promise.reject(error)
    }
  )
  return service
})
